package com.roubsite.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import com.roubsite.admin.bean.RsSystemLog;
import com.roubsite.admin.dao.rsSystemLog.IRsSystemLogDao;
import com.roubsite.admin.dao.rsSystemLog.RsSystemLogDao;
import com.roubsite.database.annotation.Dao;
import com.roubsite.database.bean.Record;
import com.roubsite.database.dao.DataSet;
import com.roubsite.security.annotation.RSSecurity;
import com.roubsite.utils.JsonUtils;
import com.roubsite.utils.StringUtils;
import com.roubsite.web.action.RSAction;

@SuppressWarnings("rawtypes")
public class RsSystemLogAction extends RSAction {
	@Dao(dataSource = "dataSource", impl = RsSystemLogDao.class)
	IRsSystemLogDao rsSystemLogDao;

	@RSSecurity
	@Override
	public void execute() throws Exception {

		this.display("/rsSystemLog/listRsSystemLog.html");
	}

	@RSSecurity
	public void doQuery() throws IOException {
		Record record = new Record();
		Map paramSet = (Map) this.getParamSet(false);
		record.fromBeanMap((Map) paramSet.get("__data__"));
		DataSet ret = rsSystemLogDao.queryd(record, (int) paramSet.get("__offset__"), (int) paramSet.get("__limit__"));
		this.print(JsonUtils.convertToString(ret));
	}

	@RSSecurity
	public void doDel() throws Exception {
		String jsonString = (String) this.getParamSet(true);
		List ids = JsonUtils.readToObject(jsonString, ArrayList.class);
		int r = rsSystemLogDao.del(ids);
		Map ret = new HashMap();
		ret.put("status", r);
		this.print(JsonUtils.convertToString(ret));
	}

	@RSSecurity
	public void doEdit() throws ServletException, IOException {
		String editState = this.g("editState");
		String method = this.g("method");
		if (StringUtils.isEmpty(editState)) {
			// 正常打开页面
			if ("UPDATE".equals(method)) {
				String id = this.g("id");
				DataSet ret = rsSystemLogDao.getInfoById(id);
				this.assign("retInfo", JsonUtils.convertToString(ret));
			}
			this.assign("method", method);
			this.display("/rsSystemLog/editRsSystemLog.html");
			return;
		}
		// 有数据提交
		String paramSet = (String) this.getParamSet(true);
		Record r = new Record();
		r.setState(Integer.parseInt(editState));
		r.fromJson(paramSet);
		int status = rsSystemLogDao.saveData(r, RsSystemLog.class);
		Map ret = new HashMap();
		ret.put("status", status);
		this.print(JsonUtils.convertToString(ret));

	}
}
